//
//  VVComponent.m
//  ComponentLinkDemo
//
//  Created by wenchao on 17/3/2.
//  Copyright © 2017年 zhenby. All rights reserved.
//

#import "VVComponent.h"
#import "CLActionEvent.h"

#import "VVActionIdentifier.h"

#import "CLDispatchCenter.h"
#import "CLDispatchCenter+VVComponent.h"

@interface VVComponent ()

@property (nonatomic) UIButton* btnComponent;

@end

@implementation VVComponent

- (void)loadView {
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"我是一个按钮" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    btn.frame = CGRectMake(10, 200, 100, 30);
    self.btnComponent = btn;
    [self.parentController.view addSubview:btn];
    
    [btn addTarget:self action:@selector(p_handleUserAction) forControlEvents:UIControlEventTouchUpInside];
    
    [CLDispatchCenter registerType:@"actionTest" target:self method:@selector(_cl_event_handle_test:)];
}

- (void)p_handleUserAction
{
    CLActionEvent* getHeight = [CLActionEvent actionWithName:kCloseServiceAction customData:@{kCloseServiceActionKeyOfUid:@(12345)}];
    NSDictionary* getService = [self performActionEvent:getHeight];
    
    CLActionEvent* context = [CLActionEvent actionWithName:@"testAction"
                                                    customData:getService];
    [self postActionEvent:context];
    
//    [CLDispatchCenter send:@"actionTest" parameters:nil];
    [CLDispatchCenter actionTest:@"1223444"];
}

- (void)actionObserverWithEvent:(CLActionEvent*)action
{
    if ([action.actionName isEqualToString:@"testAction"]) {
        NSLog(@"testAction %@",action.customData);
    } else if ([action.actionName isEqualToString:kCloseActionIdentifier]) {
        NSLog(@"recived close action %@",action.customData);
    }
}

//提供服务事件处理
- (nullable NSDictionary*)actionServiceWithEvent:(CLActionEvent*)action
{
    if ([action.actionName isEqualToString:@"testAction"]) {
        return @{@"test":@(200)};
    }
    return nil;
}

- (id)_cl_event_handle_test:(NSDictionary*)parameter
{
    NSLog(@"llll,%@",parameter);
    return nil;
}

@end
